/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Cliente;

import Entidades.Alumno;
import Entidades.Asignacion;
import Entidades.Curso;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author JeSs
 */
public class AsignacionLista extends javax.swing.JFrame {

    int idAlumno;
    List<Asignacion> resultados = new ArrayList<Asignacion>();
    /**
     * Creates new form AsignacionI
     */
    public AsignacionLista() {
        initComponents();
        try{
        initTabla();
        //ponerResultadosTabla();
        } catch (Exception e) {
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        Registros = new javax.swing.JTable();
        Eliminar = new javax.swing.JButton();
        btnRegresar = new javax.swing.JButton();
        bntAgregar = new javax.swing.JButton();
        btnModificar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Asignaciones");

        Registros.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null}
            },
            new String [] {
                "Title 1"
            }
        ));
        jScrollPane1.setViewportView(Registros);

        Eliminar.setText("Eliminar");
        Eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EliminarActionPerformed(evt);
            }
        });

        btnRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegresarActionPerformed(evt);
            }
        });

        bntAgregar.setText("Agregar");
        bntAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntAgregarActionPerformed(evt);
            }
        });

        btnModificar.setText("Modificar");
        btnModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 549, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(53, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Eliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(161, 161, 161)
                        .addComponent(bntAgregar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnModificar)
                        .addGap(63, 63, 63))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(btnRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Eliminar)
                    .addComponent(bntAgregar)
                    .addComponent(btnModificar))
                .addContainerGap(49, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void EliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EliminarActionPerformed
        
        Cliente cliente = new Cliente();
        int fila = Registros.getSelectedRow();
        int dato = Integer.parseInt(Registros.getValueAt(fila, 0).toString());
        if (cliente.eliminarAsignacion(dato, Constantes.ASIGNACION))
            JOptionPane.showMessageDialog(null, "Se ha eliminado correctamente", "Correcto", JOptionPane.INFORMATION_MESSAGE);
        else 
            JOptionPane.showMessageDialog(null, "Hubo un error al procesar la solicitud", "Error", JOptionPane.ERROR_MESSAGE);
        cliente.cerrarConexion();
        ponerResultadosTabla();
        
    }//GEN-LAST:event_EliminarActionPerformed

    private void bntAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntAgregarActionPerformed
        Cliente cliente = new Cliente();
        List<Alumno> alumnos = cliente.recibirAlumnos();
        cliente.cerrarConexion();
        cliente = new Cliente();
        List<Curso> cursos = cliente.recibirCursos();
        cliente.cerrarConexion();
        List<String> nombreAlumnos =  new ArrayList<String>();
        List<String> nombreCursos =  new ArrayList<String>();
        
        List<Integer> idAlumnos = new ArrayList<>();
        List<Integer> idCursos = new ArrayList<Integer>();
        for (Alumno alumno:alumnos){
            nombreAlumnos.add(alumno.getNombre() + " " + alumno.getPaterno());
            idAlumnos.add(alumno.getId());
        }
        for (Curso curso:cursos){
            nombreCursos.add(curso.getNombre());
            idCursos.add(curso.getId());
        }
        
        
        RegistrarAsignacion registrarH = new RegistrarAsignacion();
        registrarH.alumnos = nombreAlumnos;
        registrarH.cursos = nombreCursos;
        registrarH.idAlumnos = idAlumnos;
        registrarH.idCursos = idCursos;
        registrarH.setComboBox();
        registrarH.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_bntAgregarActionPerformed

    private void btnModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarActionPerformed
        int fila = Registros.getSelectedRow();
        int columna = Registros.getSelectedColumn();
        int dato = (int)Registros.getValueAt(fila, columna);
        
        
//        C.enviarId(dato,2,1);
//        asignacion = (Asignacion) C.recibir();
        
        ModificarAsignacion modificarH = new ModificarAsignacion();
//        modificarH.idA = asignacion.getIdAlumno();
//        modificarH.idC = asignacion.getIdCurso();
//        modificarH.Horario.setText(asignacion.getHorario());
//        modificarH.Tipo.setText(String.valueOf(asignacion.getTipoC()));
        
        modificarH.setVisible(true);
        this.setVisible(false);
        
        
    }//GEN-LAST:event_btnModificarActionPerformed

    private void btnRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegresarActionPerformed
        Menu m = new Menu();
        m.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_btnRegresarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AsignacionLista.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AsignacionLista.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AsignacionLista.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AsignacionLista.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AsignacionLista().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Eliminar;
    private javax.swing.JTable Registros;
    private javax.swing.JButton bntAgregar;
    private javax.swing.JButton btnModificar;
    private javax.swing.JButton btnRegresar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    
     private void initTabla(){
        DefaultTableModel model = (DefaultTableModel) Registros.getModel();
         String [] nombres = {"IdAlumno", "IdCurso", "Horario", "Tipo de Curso"};
        model.setColumnCount(nombres.length);
        model.setColumnIdentifiers(nombres);
    }
     
      public void ponerResultadosTabla(){
        
        Cliente cliente = new Cliente();
        DefaultTableModel model = (DefaultTableModel) Registros.getModel();
        model.setRowCount(0);
        List<Asignacion> asignaciones = cliente.recibirAsignaciones(idAlumno);
        for(Asignacion asignacion : asignaciones)
            model.addRow(asignacion.toArray());
        cliente.cerrarConexion();
      }


}
